package com.fms.controller;


import com.fms.dto.JsonRequest;
import com.fms.exception.ServiceException;
import com.fms.result.ResultData;
import com.fms.security.CheckUtils;
import com.fms.service.AttendanceService;
import com.fms.util.CastUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/attendance")
@Slf4j
public class AttendanceController {
   private final AttendanceService attendanceService;
   private final CheckUtils checkUtils;


   @GetMapping("/getRecentOfUser/{username}")
   public ResultData getRecentAttendanceOfUser(@PathVariable String username){
      log.info("获取用户最近打卡任务");
      return ResultData.success("success",attendanceService.getRecentAttendanceOfUser(username));
   }
   @PostMapping("/attended")
   public ResultData attendedOfAttendance(@RequestBody JsonRequest.AttendedOfAttendanceRequest attendedOfAttendanceRequest){
      //校验距离
      checkUtils.checkAttendanceDistance(CastUtils.castLineDistance(attendedOfAttendanceRequest.getShouldLongitude(),
              attendedOfAttendanceRequest.getShouldLatitude(),
              attendedOfAttendanceRequest.getAttendedLongitude(),
              attendedOfAttendanceRequest.getAttendedLatitude()));
      String message = "无效打卡类型";
      int type=attendedOfAttendanceRequest.getType();
      LocalDateTime inDeadline = attendedOfAttendanceRequest.getInDeadline();
      LocalDateTime now=LocalDateTime.now();
      switch (type){
         case 1:
            message="签到";
            //校验时间
            if(inDeadline.isBefore(now)||attendedOfAttendanceRequest.getStartTime().isAfter(now)){
               throw new ServiceException("此时间段无法签到");
            }
            //校验状态
            checkUtils.checkStatusOfAttendance(attendedOfAttendanceRequest.getId(), List.of(1));
            break;
         case 2:
            message="补签";
            //校验时间
            if(inDeadline.isAfter(now)){
               throw new ServiceException("此时间段无法补签");
            }
            //校验状态
            checkUtils.checkStatusOfAttendance(attendedOfAttendanceRequest.getId(), List.of(1));
            break;
         case 3:
            message="签退";
            //校验时间
            if(attendedOfAttendanceRequest.getOutDeadline().isAfter(now)){
               throw new ServiceException("此时间段无法签退");
            }
            //校验状态
            checkUtils.checkStatusOfAttendance(attendedOfAttendanceRequest.getId(), List.of(2,3));
            break;
         default:
            throw new ServiceException(message);
      }
      log.info("{}：{}",message,attendedOfAttendanceRequest);
      attendanceService.attendedOfAttendance(attendedOfAttendanceRequest);
      return ResultData.success(message+"成功");
   }

   @GetMapping("/getAllOfUser/{username}")
   public ResultData getAllAttendanceOfUser(@PathVariable String username,JsonRequest.AttendanceOfUserPageRequest attendanceOfUserPageRequest){
      log.info("获取{}全部信息:{}",username,attendanceOfUserPageRequest);
      checkUtils.checkUsernameSyntax(username,false);
      return ResultData.success("success",attendanceService.getAllAttendance(JsonRequest.AttendancePageRequest.builder()
              .page(attendanceOfUserPageRequest.getPage())
              .pageSize(attendanceOfUserPageRequest.getPageSize())
              .username(username)
              .date(attendanceOfUserPageRequest.getDate())
              .status(attendanceOfUserPageRequest.getStatus())
              .deptId(null)
              .startTime(attendanceOfUserPageRequest.getStartTime())
              .build()));
   }


   @GetMapping("/getAll")
   @PreAuthorize("hasAnyRole('经理','总经理','管理员')")
   public ResultData getAllAttendance(JsonRequest.AttendancePageRequest attendancePageRequest){
      log.info("获取全部信息:{}",attendancePageRequest);
      return ResultData.success("success",attendanceService.getAllAttendance(attendancePageRequest));
   }

   @GetMapping("/getNumber")
   @PreAuthorize("hasAnyRole('经理','总经理','管理员')")
   public ResultData getNumber(JsonRequest.AttendanceNumberRequest attendanceNumberRequest){
      log.info("统计打卡人数:{}",attendanceNumberRequest);
      return ResultData.success("success",attendanceService.getNumber(attendanceNumberRequest));
   }
}
